<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Materiels extends Model
{
  use Common;

  protected $table = 'materiel';

  protected $fillable = [
      'member_id',
      'type',
      'name',
      'overt_phone',
      'seat_phone',
      'min_age',
      'max_age',
      'sex',
      'province',
      'city',
      'license',
      'report'
  ];

  public function MaterielPhone()
  {
    return $this->hasMany(MaterielPhone::class, 'materiel_id');
  }

  public function MaterielUrl()
  {
    return $this->hasMany(MaterielUrl::class, 'materiel_id');
  }

  public function MaterielApp()
  {
    return $this->hasMany(MaterielApp::class, 'materiel_id');
  }

  /**
   * 获取layui表格数据
   * @return \Illuminate\Http\JsonResponse
   */
  public static function LayUiTableData($filter = null, $member_id = null)
  {
    $list = self::select('id', 'type', 'name', 'overt_phone', 'seat_phone', 'created_at', 'status')->withCount(['MaterielUrl','MaterielApp','MaterielPhone']);
    empty($member_id) ?: $list = $list->where('member_id', $member_id);
    if (!empty($filter)) {
      empty($filter['type']) ?: $list = $list->where('type', $filter['type']);
      empty($filter['name']) ?: $list = $list->where('name', 'like', '%' . $filter['name'] . '%');
      empty($filter['status']) ?: $list = $list->where('menu_id', $filter['status']);
    }
    $list = $list->latest('id')->paginate(self::getLimit($filter['limit']))->toArray();
    foreach ($list['data'] as &$vo) {
      $vo['materiel_type'] = self::getMaterielType($vo['type']);
      $vo['is_status'] = self::getIsStatus($vo['status']);
    }
    return self::returnLayUiData($list);
  }
}
